
# ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
# DESCRIPTION
# ______________________________________________________________________________

# This code creates Figure 3.

# Preamble ---------------------------------------------------------------------

rm(list = ls())

# Packages ---------------------------------------------------------------------

#install.packages("BiocManager")
#BiocManager::install("ggtree")

library(ggforce)
library(ggtree)

# Data -------------------------------------------------------------------------

s <- "tree(a,(b,c));"
cat(s, file = "ex.tre", sep = "\n")
tree <- read.tree("ex.tre")

# Plot -------------------------------------------------------------------------

ggtree(tree, size = 0.6) +
  geom_segment(
    x = -2,
    xend = 0,
    y = 1.75,
    yend = 1.75,
    linewidth = 0.6
  ) +
  geom_ellipse(aes(
    x0 = -1.75,
    y0 = 1.75,
    a = 0.4,
    b = 0.2,
    angle = 0
  ), fill = "white") +
  annotate(
    "text",
    x = -1.75,
    y = 1.75,
    label = "SV",
    size = 8
  ) +
  annotate(
    "label",
    x = -0.9,
    y = 1.75,
    label = "Inflation\nStage",
    size = 6,
    label.size = NA,
    lineheight = 0.9,
    color = "#005dba"
  ) +
  annotate(
    "rect",
    xmin = -0.3,
    xmax = 0.15,
    ymin = 2.3425,
    ymax = 2.7,
    fill = "white"
  ) +
  annotate(
    "label",
    x = 0,
    y = 2.5,
    label = "Outcome\nStage",
    size = 6,
    label.size = NA,
    lineheight = 0.9,
    color = "#cd2c02"
  ) +
  geom_ellipse(aes(
    x0 = 2.3,
    y0 = 1,
    a = 0.4,
    b = 0.2,
    angle = 0
  ), fill = "#a9e1ff") +
  annotate(
    "text",
    x = 2.3,
    y = 1,
    label = "Inflated 0s",
    size = 6
  ) +
  geom_ellipse(aes(
    x0 = 2.3,
    y0 = 2,
    a = 0.4,
    b = 0.2,
    angle = 0
  ), fill = "#ff9d74") +
  annotate(
    "text",
    x = 2.3,
    y = 2,
    label = "True 0s",
    size = 6
  ) +
  geom_ellipse(aes(
    x0 = 2.3,
    y0 = 3,
    a = 0.4,
    b = 0.2,
    angle = 0
  ), fill = "#f5521d") +
  annotate(
    "text",
    x = 2.3,
    y = 3,
    label = "Values > 0",
    size = 6
  ) +
  geom_ellipse(aes(
    x0 = 1,
    y0 = 2.5,
    a = 0.45,
    b = 0.2,
    m1 = 18,
    angle = 0
  ), fill = "#f5521d") +
  annotate(
    "text",
    x = 1,
    y = 2.5,
    label = "Ranking",
    size = 6,
    lineheight = 0.9
  ) +
  geom_ellipse(aes(
    x0 = 0,
    y0 = 1.75,
    a = 0.45,
    b = 0.2,
    m1 = 18,
    angle = 0
  ), fill = "#418cf0") +
  annotate(
    "text",
    x = 0,
    y = 1.75,
    label = "Separating\ntwo classes of 0s",
    size = 6,
    lineheight = 0.9
  ) +
  scale_x_continuous(expand = expansion(add = 0.01), 
                     breaks = seq(-2, 3, 0.1)) +
  scale_y_continuous(expand = expansion(add = 0.21)) +
  coord_cartesian(xlim = c(-2.1575, 2.715),
                  ylim = c(0.995, 3.025))

ggsave("figures/tree/tree.pdf", width = 12, height = 5)
